package com.zmn.oms.admin.controller.report.orderpay;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.LayuiTableResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.permit.client.annotation.FilterPermitData;
import com.zmn.oms.business.interfaces.report.ReportPayBService;
import com.zmn.oms.common.constant.OrderExportConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.report.EsReportPayQuery;
import com.zmn.oms.model.vo.report.ReportPayVO;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：wangxiaokun
 * @date ：2021/5/10 15:15
 * @description：退款支付报表
 */
@Slf4j
@Controller
@RequestMapping("/report/orderpay/refund")
public class ReportOrderPayRefundListController extends ReportOrderPayBaseController {

    @Resource
    private ReportPayBService reportPayBService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(Integer plat) {

        ModelAndView mv = new ModelAndView("report/pay/refundList");
        // 子公司列表
        mv.addObject("companyList", super.getCompanyListByDept(plat));
        // 日期类型
        mv.addObject("dateType", super.getDateType());
        mv.addObject("plat", plat);
        // 导出权限判断
        mv.addObject("export", super.exportPermit(plat, OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST));
        // 默认开始和结束时间
        mv.addObject("startTime", DateUtil.getNowFormatted(DateUtil.FORMAT_DATE_01 +" 00:00:00"));
        mv.addObject("endTime", DateUtil.getNowFormatted(DateUtil.FORMAT_DATE + " 23:59:59"));
        // 回溯时间处理
        super.setDayLimit(mv);
        return mv;
    }

    /**
     * 列表 ajax
     */
    @FilterPermitData
    @ResponseBody
    @RequestMapping(value = "/ajax/list", method = RequestMethod.POST)
    public LayuiTableResponseDTO<ReportPayVO> ajaxList(@RequestBody EsReportPayQuery query) {
        super.processQuery(query);
        query.setTradeType(PayConsts.PAY_TRADE_TYPE_REFUND);
        query.setPayStatus(PayConsts.REFUND_STATUS_DONE);
        query.setGroupFiled("companyId");
        List<ReportPayVO> reportVOS = reportPayBService.listRefundAmountSummaryByQuery(query);
        return new LayuiTableResponseDTO<>(StatusConsts.STATUS_SUCCESS, "OK", reportVOS.size(), reportVOS);
    }

    /**
     * 导出excel
     * @param query
     * @return
     */
    @PostMapping("/async/export")
    @ResponseBody
    public ResultDTO asyncExportList(@RequestBody EsReportPayQuery query) {
        this.processQuery(query);
        try {
            query.setTradeType(PayConsts.PAY_TRADE_TYPE_REFUND);
            query.setExportType(OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST);
            query.setPayStatus(PayConsts.REFUND_STATUS_DONE);
            reportPayBService.buildExportTask(query);
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }
        return ResultDTO.success();
    }

}
